import Vue from "vue"
import Vuex from "vuex"
import storage from "../common/storage"
Vue.use(Vuex)

//聊天状态管理
const state = {
    ws: false,//连接状态
    friends: [] //我的好友列表
}

const getters = {
    //获取我的好友
    getFriends(state) {
        state.friends
        var f = storage.getObj("friends")
        return f
    },
    getWsStatus(state) {
        return state.ws
    }
}

const mutations = {
    //更新我的好友数据
    updateFriends(state, data) {
        storage.setObj("friends", data)
        state.friends = data
    },
    //更新指定key的数据
    pushChat(state, data) {
        var s = storage.getObj("friends")
        s[data.key].content = data.content
        s[data.key].list.push(data)
        storage.setObj("friends", s)
        state.friends = s
    },
    //更新ws状态
    updateWs(state, data) {
        state.ws = true
    }
}

const actions = {
    //定义触发更新好友的方法
    update(context, data) {
        context.commit("updateFriends", data)
    },
    push(context, data) {
        context.commit("pushChat", data)
    },
    updateWs(context, data) {
        context.commit("updateWs", data)
    }
}

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
});